﻿using System;
using System.Collections.Generic;
using System.Text;

namespace HtmlHelper
{
    public class XhtmlElementCollection
    {
       
        private List<XhtmlElement> elements = new List<XhtmlElement>();

        private string html = "";
        public string Html
        {
            get
            {
                return html;
            }
        }

        private XhtmlElement parent = null;

        public XhtmlElementCollection()
        {

        }
        public XhtmlElementCollection(XhtmlElement parent)
        {
            this.parent = parent;
        }
        public XhtmlElement this[int index]
        {
            get
            {
                return this.elements[index];
            }
            set
            {
                value.HtmlChanged -= new EventHandler(HtmlElement_HtmlChanged);
                value.HtmlChanged += new EventHandler(HtmlElement_HtmlChanged);
                value.Parent = this.parent;
                this.elements[index] = value;
                this.constructHtml();
            }
        }
        public void Add(string html)
        {
            XhtmlElement he = new XhtmlElement(html);
            he.HtmlChanged += new EventHandler(HtmlElement_HtmlChanged);
            he.Parent = this.parent;
            this.elements.Add(he);
            this.constructHtml();
        }
        public void Add(XhtmlElement he)
        {
            he.HtmlChanged -= new EventHandler(HtmlElement_HtmlChanged);
            he.HtmlChanged += new EventHandler(HtmlElement_HtmlChanged);
            he.Parent = this.parent;
            this.elements.Add(he);
            this.constructHtml();
        }
        public void Remove(XhtmlElement he)
        {
            this.elements.Remove(he);
            he.Parent = null;
            this.constructHtml();
        }
        public void Clear()
        {
            this.elements.Clear();
            this.constructHtml();
        }
        public int Count
        {
            get { return this.elements.Count; }
        }
        public int IndexOf(XhtmlElement element)
        {
            return this.elements.IndexOf(element);
        }
        public bool Contains(XhtmlElement element)
        {
            return this.elements.Contains(element);
        }

        private void HtmlElement_HtmlChanged(object sender,EventArgs e) 
        {
            this.constructHtml();
        }
        private void constructHtml()
        {
                string s = "";
                for (int i = 0; i < this.elements.Count; i++)
                {
                    s += " " + this.elements[i].Html;
                }
                this.html = s;
                this.OnHtmlChanged();
        }
        public event EventHandler HtmlChanged;
        protected void OnHtmlChanged()
        {
            if (this.HtmlChanged != null)
            {
                this.HtmlChanged(this, new EventArgs());
            }
        }

    }
}
